var memberUtil=require('../../utils/memberUtil');
var MD5 = require("../../utils/md5.js");
var app = getApp();
var kdonekey="ExOltDfL9254" ;
var kdonecustomer="94EB2410AF4F4DEE4899BA1BF2FC47F2";
Page({

  /**
   * 页面的初始数据
   */
  data: {
      orderInfo:null,
      likes:[],
      serverImageUrl:app.globalData.baseUrl,
      wuliuInfo:[],
      state:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that=this;
      var kuaidiInfo=options.kuaidiInfo;
      var resss=(wx.getStorageSync("loginUserInfo").userInfo==undefined?"":wx.getStorageSync("loginUserInfo").userInfo);
      var kdInfo=wx.getStorageSync("kuaidiInfo");
      that.setData({
          userInfo:resss,
          orderInfo:kdInfo
      })
      if(that.data.userInfo.id==undefined) {
          memberUtil.getLogin(app,0,1,that,2);
      }
  },
   /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var that=this;
      that.getLike();
      that.kuaidiselectInfo();
  },
    //你可能喜欢
    getLike: function (){
        var that=this;
        //获取地址
        wx.request({
            url: app.globalData.baseUrl+'/goods/mayBeLike',
            header: app.globalData.headerPost,
            method: 'GET',
            success:function(res){
                var likeObj="";
                if(res.data.length>0) {
                    that.setData({
                        likes: res.data
                    })
                }

            }
        })
    },
    //查询快递信息
    kuaidiselectInfo:function(){
        var that=this;
        var param=JSON.stringify({com:that.data.orderInfo.logisticsNameCode,
            num:that.data.orderInfo.logisticsCode});
        var sign= MD5.md5_2(param+kdonekey+kdonecustomer).toUpperCase();
        // var sign= MD5.MD5(param+kdonekey+kdonecustomer).toString().toUpperCase();
        var json = {
            customer:kdonecustomer,
            sign: sign,
            param:param
        };
        console.log("请求参数="+JSON.stringify(json))
        wx.request({
            url: 'https://poll.kuaidi100.com/poll/query.do?customer='+kdonecustomer+"&sign="+sign+"&param="+param,
            data: {},
            method:'POST',
            success: function(res){
                console.log("接口返回参数=res"+JSON.stringify(res))
                var wuliuxinxi=[];
                if(res.data.status==200) {
                    var wuliuInfo = res;
                    if(res.data.state>=0){
                        for(var a in res.data.data){
                            if(Number(res.data.state)==3 && Number(a)==0) {
                                wuliuxinxi.push({
                                    icon: "/images/icon/log5.png",
                                    dateTime: that.timeFormat(res.data.data[a].time),
                                    hoursTime: that.timeFormat2(res.data.data[a].time),
                                    context: res.data.data[a].context,
                                    state:"已签收"
                                })
                            }else{
                                wuliuxinxi.push({
                                    icon: "/images/icon/log22.jpg",
                                    dateTime: that.timeFormat(res.data.data[a].time),
                                    hoursTime: that.timeFormat2(res.data.data[a].time),
                                    context: res.data.data[a].context,
                                    state:"运输中"
                                })
                            }
                        }
                    }
                    that.setData({
                        wuliuInfo: wuliuxinxi,
                        state:res.state
                    })
                }else{
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    })
                    return false;
                }
            },
            fail:function (res) {
                console.log("出错="+JSON.stringify(res))
            }
        })

    },
//时间截取
    timeFormat:function(time){
      var month=time.substring(5,7);
      var date=time.substring(8,10);
      return month+'/'+date;
    },
    //页面跳转
    goodsDetailNavigate:function(e){
        wx.showLoading({
            title: '数据加载中',
            mask: true
        })
        var goodsId=e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/shop/shopDt?goodsId='+goodsId
        })
        wx.hideLoading();
    },
    //时间截取
    timeFormat2:function(time){
        var dateTime=time.substring(11,16);
        return dateTime;
    },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})